#include <iostream>
#include <vector>

using namespace std;

// bad
class Solution1
{
public:
    // -7,-1,0,-2,1,3,8,-2,-6,-1,-10,-6,-6,8,-4,-9,-4,1,4,-9
    int maxAbsoluteSum(vector<int> &nums)
    {
        vector<int> sumNum;
        int curSum = nums[0];
        for (int i = 1; i < nums.size(); i++)
        {
            if ((nums[i] >= 0 && nums[i - 1] >= 0) || (nums[i] < 0 && nums[i - 1] < 0))
            {
                curSum += nums[i];
                continue;
            }
            sumNum.push_back(curSum);
            curSum = nums[i];
        }
        sumNum.push_back(curSum);
        int pre = sumNum[0];
        int ans = abs(pre);
        for (int i = 1; i < sumNum.size(); i++)
        {
            if (abs(pre + sumNum[i]) > abs(sumNum[i]))
            {
                pre += sumNum[i];
            }
            else
            {
                pre = sumNum[i];
            }
            ans = max(ans, abs(pre));
        }
        return ans;
    }
};

class Solution
{
public:
    // 前缀和最大值 - 前缀和最小值即为答案
    int maxAbsoluteSum(vector<int> &nums)
    {
        int maxArrSum = nums[0];
        int minArrSum = nums[0];
        int ans = abs(nums[0]);
        for (int i = 1; i < nums.size(); i++)
        {
            if (maxArrSum + nums[i] > nums[i])
            {
                maxArrSum += nums[i];
            }
            else
            {
                maxArrSum = nums[i];
            }
            if (minArrSum + nums[i] < nums[i])
            {
                minArrSum += nums[i];
            }
            else
            {
                minArrSum = nums[i];
            }
            ans = max(ans, max(abs(minArrSum), maxArrSum));
        }
        return ans;
    }
};

int main()
{
    Solution solution{};
    return 0;
}